<!--
 * @Description:
 * @Author: linjz
 * @Date: 2021-06-25 10:25:00
 * @LastEditors: linjz
 * @LastEditTime: 2021-06-28 18:17:11
-->
<template>
  <!-- 资讯分类管理 -->
  <div>
    <!-- 新增按钮 -->
    <el-button type="primary" @click="toAdd">新增</el-button>

    <!-- 表格区域 -->
    <div>
      <!-- {{ tableData }} -->
      <el-table :data="tableData">
        <el-table-column
          type="index"
          label="序号"
          align="center"
          header-align="center"
        />
        <el-table-column prop="name" label="名称" align="center" />
        <el-table-column prop="no" label="排序号" align="center" />
        <el-table-column prop="description" label="描述" align="center" />
        <el-table-column prop="address" label="操作" align="center">
          <template slot-scope="scope">
            <el-button type="text" @click="toEdit(scope.row)">编辑</el-button>
            <el-button
              type="text"
              class="style_color"
              @click="toDelete(scope.row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 模态框 -->
    <div>
      <el-dialog
        :title="title"
        :visible.sync="visible"
        width="40%"
        @close="toClose"
      >
        <!-- {{ ruleForm }} -->
        <el-form
          ref="ruleForm"
          :model="ruleForm"
          :rules="rules"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item label="栏目名称" prop="name">
            <el-input v-model="ruleForm.name" />
          </el-form-item>
          <el-form-item label="序号" prop="no">
            <el-input v-model.number="ruleForm.no" />
          </el-form-item>
          <el-form-item label="描述">
            <el-input v-model="ruleForm.description" type="textarea" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="visible = false">取 消</el-button>
          <el-button type="primary" @click="toSave">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { findAll, deleteById, saveOrUpdate } from "@/api/infoCategory";

export default {
  data() {
    return {
      // 表格内容
      tableData: {},
      // 模态框标题
      title: "",
      // 模态框显示
      visible: false,
      // 表单数据
      ruleForm: {},
      // 表单验证
      rules: {
        name: [{ required: true, message: "请输入栏目名称", trigger: "blur" }],
        no: [
          { type: "number", message: "序号必须为数字值", trigger: "change" },
        ],
      },
    };
  },
  computed: {},
  created() {
    this.findAllData();
  },
  mounted() {},
  methods: {
    // 查询所有资讯分类
    async findAllData() {
      const res = await findAll();
      // console.log(res.data);
      this.tableData = res.data;
    },
    // 重置表单验证
    toClose() {
      this.$refs.ruleForm.resetFields();
    },
    // 新增按钮点击处理事件
    toAdd() {
      this.title = "新增栏目信息";
      this.ruleForm = {};
      this.visible = true;
    },
    // 确定按钮点击处理事件
    async toSave() {
      this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          const res = await saveOrUpdate(this.ruleForm);
          // console.log(res);
          if (res.status === 200) {
            this.$notify({
              title: "成功",
              message: res.message,
              type: "success",
            });
            this.findAllData();
            this.visible = false;
          }
        } else {
          return false;
        }
      });
    },
    // 编辑按钮点击处理事件
    toEdit(row) {
      this.title = "编辑栏目信息";
      this.ruleForm = { ...row };
      this.visible = true;
    },
    // 删除按钮点击处理事件
    async toDelete(id) {
      const res = await deleteById({ id });
      // console.log(res);
      if (res.status === 200) {
        this.$notify({
          title: "成功",
          message: res.message,
          type: "success",
        });
      }
      this.findAllData();
    },
  },
};
</script>
<style scoped>
.style_color {
  color: #e65d6e;
}
</style>
